package com.erp.serviceImpl.purchaseServiceImpl;

import com.erp.dao.DepotHeadMapper;
import com.erp.pojo.DepotHead;
import com.erp.pojo.DepotHeadExample;
import com.erp.service.purchaseService.DepotHeadOutService;
import com.erp.utils.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepotHeadOutServiceImpl implements DepotHeadOutService {
    @Autowired
    private DepotHeadMapper depotHeadMapper;

    @Override
    public Msg getAllDepotHead(int page,int limit,String number, String operpersonname, Date billTimeBefore, Date billTimeBack) {
        DepotHeadExample example = new DepotHeadExample();
        DepotHeadExample.Criteria criteria = example.createCriteria();
        if(number!=null){
            number = "%"+number+"%";
            criteria.andNumberLike(number);
        }
        if(operpersonname!=null){
            operpersonname = "%"+operpersonname+"%";
            criteria.andOperpersonnameLike(operpersonname);
        }
        if(billTimeBefore!=null && billTimeBack!=null){
            criteria.andCreatetimeBetween(billTimeBefore,billTimeBack);
        }else if(billTimeBefore!=null){
            criteria.andCreatetimeGreaterThanOrEqualTo(billTimeBefore);
        } else if(billTimeBack!=null){
            criteria.andCreatetimeLessThanOrEqualTo(billTimeBack);
        }
        String subtype="采购退货";
        criteria.andSubtypeEqualTo(subtype);
//        准备分页
        PageHelper.startPage(page,limit);
//        检查是否为条件查询
        List<DepotHead> depotHeads = depotHeadMapper.selectByExample(example);
//        进行分页
        PageInfo<DepotHead> pageinfo = new PageInfo<>(depotHeads, 10);
        Msg msg = new Msg();
        msg.setCode(0);
        msg.setMsg("");
        msg.setCount(pageinfo.getTotal());
        msg.setData(pageinfo.getList());
        return msg;
    }

    @Override
    public void addDepotHead(DepotHead depotHead) {
        depotHeadMapper.insert(depotHead);
    }

    @Override
    public void deleteDepoetHead(Long id) {
        depotHeadMapper.deleteByPrimaryKey(id);
    }
}
